class Link < ActiveRecord::Base
  #from_row, from_column, to_row, to_column, grid_id

  belongs_to :grid

  composed_of :from, :mapping => [ %w(from_row row), %w(from_column column) ], :converter => :adapt, :class_name => 'Location'
  composed_of :to, :mapping => [ %w(to_row row), %w(to_column column) ], :converter => :adapt, :class_name => 'Location'

  def to_hash
    ToHash.eval_attributes(self, [:from, :to])
  end

  def self.destroy_at_locations(location1, location2)
    self.destroy_all(
        ['(from_row = ? AND from_column = ? AND to_row = ? AND to_column = ?) OR
        (from_row = ? AND from_column = ? AND to_row = ? AND to_column = ?)',
        location1[:row], location1[:column], location2[:row], location2[:column],
        location2[:row], location2[:column], location1[:row], location1[:column]])
  end
  
  def self.destroy_at_dimensions(rows, columns)
    self.destroy_all(
      ['from_row >= ? OR from_column >= ? OR to_row >= ? OR to_column >= ?', rows, columns, rows, columns])
  end



end
